<?php
class CmsController extends ControllerAction
{
	public function init()
	{
		$this->setTable($this->_getParam('t'))
			->setSchema('ecommerce')
			->setCms();
		
		// CMS Css
		$this->view->headLink()
			->appendStylesheet($this->view->cssPath.'/cms.css');
	}
	
	public function indexAction()
	{
		$this->view->tables = $this->getCms()->getTablesList();
	}
	
	public function listAction()
	{
		if($this->getTable() == null) {
			$this->_redirect('/cms');
		}
		
		// Filters
		$session = new Zend_Session_Namespace($this->getTable());
		if($this->getRequest()->isPost()) {
			if($this->getRequest()->getPost('clear', false)) {
				$session->unsetAll();
			} else {
				$session->filters = $this->getRequest()->getPost(
					'search', array()
				);
			}
		}
		
		$allData = $this->getCms()->getTableData(
			(empty($session->filters)) ? array() : $session->filters
		);
		
		$columns = $this->getCms()->columnsToSearch($allData['data']);
		
		$this->view->filters = $session->filters;
		$this->view->tableData = $allData['data'];
		
		// The ids are table, column and value parameters to detail each row
		$this->view->ids = $allData['ids'];
		
		// Columns
		$this->view->fields = $columns;
		
		// Ordenation Params
		$this->view->order = $this->getCms()->orderColumns($columns);
	}
	
	public function detailAction()
	{
		if($this->getTable() == null) {
			$this->_redirect('/cms');
		}
		
		$allData = $this->getCms()->getTableRow();
		
		$this->view->data = $allData['data'][0];
		$this->view->columns = $this->getCms()
			->columnsToSearch($allData['data']);
		
		$this->view->params = $this->getRequest()->getParams();
	}
	
	public function editAction()
	{
		$this->newAction(true);
	}
	
	public function newAction($edit = false)
	{
		$this->view->data = $this->getCms()->getColumns();
		
		$post = array();
		
		$matchColumnsToPost = false;
		
		if($this->getRequest()->isPost())
		{
			$post = $this->getRequest()->getPost();
			
			$result = $this->getCms()->save($post);
			
			if(isset($result['errors'])) {
				if(isset($post['nm'])) {
					$matchColumnsToPost = true;
				}
				
				$this->view->data['errors'] = $result['messages'];
			} else {
				// @TODO: Adicionar mensagem de sucesso
				$this->_redirect('/cms/list/t/'.$this->getTable());
			}
		} elseif($edit) {
			$post = $this->getCms()->getTableRowToEdit($this->view->data);
			$matchColumnsToPost = true;
		}
		
		if($matchColumnsToPost && isset($post['nm'])) {
			// Find post mn fields to each nm table in set
			$this->view->data['nms'] = $this->getCms()->matchColumnsToPost(
				$this->view->data['nms'], $post['nm']
			);
		}
		
		$this->view->data['post'] = $post;
	}
	
	public function deleteAction()
	{
		$confirm = $this->getRequest()->getPost('confirm', false);
		
		// If have confirmation 
		if($confirm == true)
		{
			$response = $this->getCms()->deleteRow();
			if($response['result'] == false) {
				// @TODO: Mensagem de erro do sistema aqui
//				$response['error_message']
			}
			
			$this->_redirect('/cms/list/t/'.$this->getTable());
		}
		
		$this->view->confirm = $confirm;
		$this->view->params = $this->getRequest()->getParams();
	}
}